package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.Site;
import com.example.buildtable.service.SiteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "站点")
@RequestMapping(value = "/site/")
public class SiteController {
    @Autowired
    private SiteService siteService;
    
    @Operation(summary = "查询所有站点数据")
    @GetMapping(value = "/getAllSite")
    public R getAllSite() {
        List<Site> siteList = siteService.list();
        return R.ok("查询所有站点数据成功").data("siteList", siteList);
    }
    
    @Operation(summary = "增加站点")
    @PostMapping(value = "/addSite")
    public R addSite(Site site) {
        boolean flag = siteService.save(site);
        if (flag) {
            return R.ok("增加站点成功");
        } else {
            return R.ok("增加站点失败");
        }
    }

    @Operation(summary = "删除站点")
    @GetMapping(value = "/deleteSite/{siteId}")
    public R deleteSite(@PathVariable("siteId") Integer siteId) {
        boolean flag = siteService.removeById(siteId);
        if (flag) {
            return R.ok("删除站点成功");
        } else {
            return R.ok("删除站点失败");
        }
    }

    @Operation(summary = "按照站点查看")
    @GetMapping(value = "/getSiteById/{siteId}")
    public R getSiteById(@PathVariable("siteId") Integer siteId) {
        Site site = siteService.getById(siteId);
        if (site != null) {
            return R.ok("按照站点查看成功").data("site", site);
        } else {
            return R.error("按照站点查看失败");
        }
    }

    @Operation(summary = "修改站点")
    @PostMapping(value = "/updateSite")
    public R updateSite(Site site) {
        boolean flag = siteService.updateById(site);
        if (flag) {
            return R.ok("修改站点成功");
        } else {
            return R.ok("修改站点失败");
        }
    }
}